﻿-- Author      : Kurapica
-- Create Date : 6/13/2008 9:27:45 PM
   
-- Texture
do
	-- Check Version
	local version = 1
	if not IGAS:RegisterFile("IGAS.GUI.Widget.Texture", version) then
		return
	end

    local getmetatable = getmetatable
    local pairs = pairs
    local ipairs = ipairs
    local type = type
    local error = error
    local CreateFrame = CreateFrame
    
	local _WidgetName = "Texture"
    local _Base = "LayeredRegion"

	local _GUI = IGAS.GUI
	local _Widget = IGAS.GUI.Widget
    local _BaseFunc = IGAS.GUI.Base.Func
	local _PropertyType = IGAS.GUI.Base.PropertyType   
	
	-- FuncProxy
	local _FuncProxy = {		
	}
	
	-- Property
	local _Property = {
		--- BlendMode
		["BlendMode"] = {
			["Set"] = function(self, mode)
				self.__UI:SetBlendMode(mode)
			end,

			["Get"] = function(self)
				return self.__UI:GetBlendMode()
			end,

			["Type"] = _PropertyType.AlphaMode,
		},
		--- Desaturated
		["Desaturated"] = {		
			["Set"] = function(self, desaturation)
				local shaderSupported = self.__UI:SetDesaturated(desaturation);
				if ( not shaderSupported ) then
					if ( desaturation ) then
						self.__UI:SetVertexColor(0.5, 0.5, 0.5);
					else
						self.__UI:SetVertexColor(1.0, 1.0, 1.0);
					end				
				end
			end,

			["Get"] = function(self)
				return (self.__UI:IsDesaturated() and true) or false
			end,

			["Type"] = _PropertyType.Boolean, 
		},
		--- Texture
		["Texture"] = {
			["Set"] = function(self, texture)
				if type(texture) == "string" then
					self.__UI:SetTexture(texture)
				elseif type(texture) == "table" then
					self.__UI:SetTexture(texture.red,texture.green,texture.blue,texture.alpha)
				end
			end,

			["Get"] = function(self)
				local colorTable = {}
				colorTable.red, colorTable.green, colorTable.blue, colorTable.alpha = self.__UI:GetTexture()
				
				if colorTable.green then
					return colorTable
				else
					return colorTable.red
				end
			end,

			["Type"] = _PropertyType.Texture, 
		},
		--- VertexColor
		["VertexColor"] = {
			["Set"] = function(self, color)
				self:SetVertexColor(color.red, color,green, color.blue, color.alpha)
			end,
			
			["Get"] = function(self)
				local colorTable = {}
				colorTable.red, colorTable.green, colorTable.blue, colorTable.alpha = self.__UI:GetVertexColor()
				
				return colorTable
			end,

			["Type"] = _PropertyType.Color,		
		},
	}

    local _New = function(_parent, layer, inheritsFrom)
    	if not (_parent and _parent["CreateTexture"]) then
    		return nil
    	end
    	
        -- New Frame
		local frame = _parent:CreateTexture(nil, layer, inheritsFrom)
		
        return frame
    end
	
    local _WidgetInfo = {
        ["WidgetName"] = _WidgetName,
        ["Base"] = _Base,
        ["FuncProxy"] = _FuncProxy,
        ["Property"] = _Property,
        ["New"] = _New,
    }
    
    _Widget[_WidgetName] = _GUI.NewWidget(_WidgetInfo)
    
	------------------------------------------------------
	--------------- Scan new function --------------------
    ------------------------------------------------------
	local _frame = CreateFrame("Frame",nil,UIParent)
    local _text = _frame:CreateTexture()
    
	local _oriMeta = getmetatable(_text)
	_oriMeta = _oriMeta.__index
    
    local _func = _Widget[_WidgetName]["FuncProxy"]
	-- Other Function
	for _f in pairs(_oriMeta) do
		if not _func[_f] and type(_oriMeta[_f]) == "function" then
			_func[_f] = function(self, ...)
                return _BaseFunc.GetVals(self.__UI[_f](self.__UI, _BaseFunc.GetArgs(...)))
			end
		end
	end
	
	-- Clear
	_oriMeta = nil
    _frame:Hide()
	_frame = nil
end																 